﻿@using Fierce.MvcExtend.Controls.Manage

<div class="easyui-layout" data-options="fit:true">

    <div data-options="region:'north',border:false,height:112">
        <div class="document-name">@ViewBag.Work.Menu.Name</div>
        <div class="document-toolbar">@Easyui.Toolbar(ViewBag.Work)</div>
    </div>

    <div data-options="region:'center',border:false"><table id="igrid" /></div>

</div>

<script type="text/javascript">
    $(function () {
        //表格初始化
        var gridInit = function () {
            $("#igrid").treegrid({
                url: "/basic/menu/grid",
                nowrap: false,
                rownumbers: true,
                fit: true,
                singleSelect: true,
                idField: "ID",
                treeField: "Name",
                parentField: "Pid",
                columns: [[
                    { field: "Name", title: "名称", width: 200, align: "left" },
                    { field: "Type", title: "类型", width: 80, align: "center" },
                    { field: "Module", title: "模块", width: 80, align: "center" },
                    { field: "Tag", title: "标记", width: 150, align: "center" },
                    { field: "Link", title: "连接", width: 250, align: "center" },
                    { field: "Sequence", title: "排序", width: 50, align: "center" },
                    {
                        field: "IsEnabled", title: "启用", width: 50, align: "center",
                        formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                    }
                ]]
            });
        }();
        //添加
        var insert = function () {
            var row = $("#igrid").treegrid("getSelected");
            if (row) {
                mutual("添加", null, row.ID);
            }
            else { mutual("添加", null, null); }
        };
        //修改
        var update = function () {
            var row = $("#igrid").treegrid("getSelected");
            if (row) {
                mutual("修改", row.ID, null);
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //删除
        var remove = function () {
            var row = $("#igrid").treegrid("getSelected");
            if (row) {
                manage.confirm("确认删除?", function (del) {
                    if (del) {
                        manage.ajax({
                            url: "/basic/menu/remove",
                            type: "post", data: { id: row.ID },
                            success: function (result) {
                                manage.slide(result.message);
                                if (result.success) { reload(); }
                            }
                        });
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //实体操作
        var mutual = function (title, id, pid) {
            var href = "/basic/menu/page?" + $.param({ id: id, pid: pid });
            var d = manage.dialog({
                title: title, href: href, width: 420,
                bodyCls: "theme-container",
                handler: function () {
                    manage.form({
                        dialog: d, formid: "#dform",
                        url: "/basic/menu/save",
                        success: function (result) { reload(); }
                    });
                },
                onLoad: function () { formInit(id); }
            });
        };
        //弹窗初始化
        var formInit = function (id) {
            $("#iptIcon").textbox({
                required: true, editable: false,
                icons: [{
                    iconCls: "icon-application-view-icons",
                    handler: function () {
                        var d = manage.dialog({
                            title: "选择图标",
                            id: "iptIconBox",
                            width: 542, height: 460,
                            iconCls: "icon-bricks",
                            href: "/content/easyui/extensions/icons/select.html",
                            onLoad: function () {
                                $("span.list-span", "#iptIconBox").click(function () {
                                    var value = $(this).data("value");
                                    d.dialog("close");
                                    $("#iptIcon").textbox("setValue", value);
                                });
                            }
                        });
                    }
                }]
            });
            $("#iptPid").combotree({
                required: true, editable: false,
                url: "/basic/menu/tree",
                onLoadSuccess: function (node, data) {
                    if (id) {
                        var etree = $("#iptPid").combotree("tree");
                        etree.tree("remove", etree.tree("find", id).target);
                    }
                }
            });
        };
        //排序上移
        var desc = function () {
            order("desc");
        };
        //排序下移
        var asc = function () {
            order("asc");
        };
        //排序操作
        var order = function (order) {
            var row = $("#igrid").treegrid("getSelected");
            if (row) {
                manage.ajax({
                    url: "/basic/menu/setorder",
                    type: "post", data: { id: row.ID, order: order, pid: row.Pid },
                    success: function (result) {
                        manage.slide(result.message);
                        if (result.success) {
                            $("#igrid").treegrid("reload");
                        }
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //启用
        var enable = function () {
            enableOrDisable(true);
        };
        //禁用
        var disable = function () {
            enableOrDisable(false);
        };
        //启用禁用
        var enableOrDisable = function (bool) {
            var row = $("#igrid").treegrid("getSelected");
            if (row) {
                manage.ajax({
                    url: "/basic/menu/enableordisable",
                    type: "post", data: { id: row.ID, bit: bool },
                    success: function (result) {
                        manage.slide(result.message);
                        if (result.success) {
                            $("#igrid").treegrid("reload");
                        }
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //刷新列表
        var reload = function () {
            $("#igrid").treegrid("unselectAll");
            $("#igrid").treegrid("reload");
        };
        //功能分配操作
        var assign = function () {
            var row = $("#igrid").treegrid("getSelected");
            if (row) {
                var d = manage.dialog({
                    title: "配置功能",
                    width: 828, height: 524,
                    href: "/basic/menu/assign",
                    handler: function () {
                        var rows = $("#mfgrid").datagrid("getRows");
                        manage.ajax({
                            url: "/basic/menu/setassign", type: "post",
                            data: { id: row.ID, rows: JSON.stringify(rows) },
                            success: function (result) {
                                if (result.success) {
                                    d.dialog("close");
                                    manage.slide(result.message);
                                }
                                else { manage.msg(result.message); }
                            }
                        });
                    },
                    onLoad: function () { assignInit(row.ID); }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //功能分配初始化
        var assignInit = function (id) {
            $("#fgrid").datagrid({
                url: "/basic/function/allgrid",
                title: "全部功能（双击添加）",
                nowrap: false,
                rownumbers: true,
                idField: "ID",
                fit: true,
                border: false,
                singleSelect: true,
                columns: [[
                    {
                        field: "Icon", title: "图标", width: 40, align: "center",
                        formatter: function (value, row, index) {
                            if (value) { return manage.icon(value); }
                        }
                    },
                    { field: "Name", title: "名称", width: 75, align: "center" },
                    { field: "Type", title: "类型", width: 60, align: "center" },
                    { field: "Remark", title: "描述", width: 280, align: "center" }
                ]],
                onDblClickRow: assignAdded
            });
            $("#mfgrid").datagrid({
                url: "/basic/function/allgridbymenu",
                loadMsg: "Loading...",
                queryParams: { menuid: id },
                title: "已选功能（双击删除）",
                nowrap: false,
                rownumbers: true,
                idField: "ID",
                fit: true,
                border: false,
                singleSelect: true,
                toolbar: "#mftools",
                columns: [[
                    {
                        field: "Icon", title: "图标", width: 40, align: "center",
                        formatter: function (value, row, index) { return manage.icon(value); }
                    },
                    {
                        field: "Name", title: "名称", width: 82, align: "center"
                    }
                ]],
                onDblClickRow: assignRemove
            });
            $("#mfmenuid").val(id);            
            manage.buttonInit("basicmenuassign", function (f) { return eval(f); });
        };
        //功能添加
        var assignAdded = function (index, row) {
            if (row) {
                var rows = $("#mfgrid").datagrid("getRows");
                for (var i = 0; i < rows.length; i++) {
                    if (row.ID == rows[i].ID) { return false; }
                }
                $("#mfgrid").datagrid("appendRow", {
                    ID: row.ID, Icon: row.Icon, Name: row.Name
                });
            }
        };
        //功能删除
        var assignRemove = function (index, row) {
            if (row) {
                $("#mfgrid").datagrid("deleteRow", index);
            }
        };
        //排序上移
        var assignOrderUp = function () {
            assignOrder("up");
        };
        //排序下移
        var assignOrderDown = function () {
            assignOrder("down");
        };
        //功能排序
        var assignOrder = function (order) {
            var row = $("#mfgrid").datagrid("getSelected");
            if (row) {
                var rowindex = $("#mfgrid").datagrid("getRowIndex", row);
                var rows = $("#mfgrid").datagrid("getRows");
                if (order == "up") {
                    if (rowindex > 0) {
                        var newindex = rowindex - 1;

                        var targetRow = rows[newindex];
                        var currentRow = rows[rowindex];

                        rows[newindex] = currentRow;
                        rows[rowindex] = targetRow;

                        $("#mfgrid").datagrid("loadData", rows);
                        $("#mfgrid").datagrid("selectRow", newindex);
                    }
                    else { manage.slide("到顶啦!"); }
                }
                else {
                    if (rowindex < rows.length - 1) {
                        var newindex = rowindex + 1;

                        var targetRow = rows[newindex];
                        var currentRow = rows[rowindex];

                        rows[newindex] = currentRow;
                        rows[rowindex] = targetRow;

                        $("#mfgrid").datagrid("loadData", rows);
                        $("#mfgrid").datagrid("selectRow", newindex);
                    }
                    else { manage.slide("到底啦!"); }
                }
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //功能清除
        var assignClear = function () {
            $("#mfgrid").datagrid("unselectAll");
            $("#mfgrid").datagrid("loadData", []);
        };
        //按钮事件
        manage.buttonInit("basicmenu", function (f) { return eval(f); });
    });
</script>